package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.PartyProbationary;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PartyFormal;
import com.ruoyi.system.service.IPartyFormalService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 正式党员转正Controller
 * 
 * @author ruoyi
 * @date 2025-10-16
 */
@RestController
@RequestMapping("/system/formal")
public class PartyFormalController extends BaseController
{
    @Autowired
    private IPartyFormalService partyFormalService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询正式党员转正列表
     */
    //("@ss.hasPermi('system:formal:list')")
    @GetMapping("/list")
    public TableDataInfo list(PartyFormal partyFormal)
    {
        startPage();
        List<PartyFormal> list = partyFormalService.selectPartyFormalList(partyFormal);
        return getDataTable(list);
    }

    /**
     * 导出正式党员转正列表
     */
    //("@ss.hasPermi('system:formal:export')")
    @Log(title = "正式党员转正", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PartyFormal partyFormal)
    {
        List<PartyFormal> list = partyFormalService.selectPartyFormalList(partyFormal);
        ExcelUtil<PartyFormal> util = new ExcelUtil<PartyFormal>(PartyFormal.class);
        util.exportExcel(response, list, "正式党员转正数据");
    }

    /**
     * 获取正式党员转正详细信息
     */
    //("@ss.hasPermi('system:formal:query')")
    @GetMapping(value = "/{formalId}")
    public AjaxResult getInfo(@PathVariable("formalId") Long formalId)
    {
        return success(partyFormalService.selectPartyFormalByFormalId(formalId));
    }

    /**
     * 新增正式党员转正
     */
    //("@ss.hasPermi('system:formal:add')")
    @Log(title = "正式党员转正", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PartyFormal partyFormal)
    {
        return toAjax(partyFormalService.insertPartyFormal(partyFormal));
    }

    /**
     * 修改正式党员转正
     */
    //("@ss.hasPermi('system:formal:edit')")
    @Log(title = "正式党员转正", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PartyFormal partyFormal)
    {
        return toAjax(partyFormalService.updatePartyFormal(partyFormal));
    }

    /**
     * 审核正式党员转正申请
     */
    //("@ss.hasPermi('system:formal:edit')")
    @Log(title = "正式党员转正审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody PartyFormal partyFormal)
    {
        partyFormal.setAuditTime(new Date());
        String auditStatus = partyFormal.getAuditStatus();

        PartyFormal que = partyFormalService.selectPartyFormalByFormalId(partyFormal.getFormalId());
        // 如果审核通过，更新用户的当前阶段为正式党员
        if ("1".equals(auditStatus)) {
            partyFormal.setFormalDate(new Date());
            sysUserService.updateUserCurrentStage(que.getUserId(), "4");
        }
        
        return toAjax(partyFormalService.updatePartyFormal(partyFormal));
    }

    /**
     * 删除正式党员转正
     */
    //("@ss.hasPermi('system:formal:remove')")
    @Log(title = "正式党员转正", businessType = BusinessType.DELETE)
	@DeleteMapping("/{formalIds}")
    public AjaxResult remove(@PathVariable Long[] formalIds)
    {
        return toAjax(partyFormalService.deletePartyFormalByFormalIds(formalIds));
    }
}
